package com.whisperly.echotalk.starryb.schemas;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.LocaleList;
import android.preference.PreferenceManager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.whisperly.echotalk.starryb.R;
import com.whisperly.echotalk.starryb.utils.AppsManager;

import java.util.Locale;

public class SplashPage extends AppCompatActivity {

    private static final int SPLASH_TIME = 2500;

    //Hooks
    View first;
    TextView title, tips;

    private ProgressBar progressBar;

    //Animations
    Animation top, bottom, middle;

    @Override
    protected void attachBaseContext(Context newBase) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(newBase);
        String languageCode = prefs.getString("language", "en");
        super.attachBaseContext(updateResources(newBase, languageCode));
    }

    public Context updateResources(Context context, String languageCode) {
        Locale locale = new Locale(languageCode);
        Locale.setDefault(locale);

        Resources res = context.getResources();
        Configuration config = new Configuration(res.getConfiguration());
        config.setLocale(locale);
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
        context = context.createConfigurationContext(config);
        return context;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.initiate);
        AppsManager.addActivity(this);
        bottom = AnimationUtils.loadAnimation(this,R.anim.bottom);
        middle = AnimationUtils.loadAnimation(this,R.anim.middle);
        top = AnimationUtils.loadAnimation(this,R.anim.top);
        title = findViewById(R.id.splash_title);
        tips = findViewById(R.id.splash_tips);
        SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
        boolean hasAgreed = prefs.getBoolean("hasAgreed", false);

        if (!hasAgreed) {
            showAgreementDialog();
        }else {
            startMainActivity();
        }

    }

    private void startMainActivity() {
        title.setAnimation(middle);
        tips.setAnimation(top);
        new android.os.Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                Intent intent = new Intent(SplashPage.this, AI.class);
                startActivity(intent);
                finish();
            }
        },SPLASH_TIME);
    }

    private void showAgreementDialog() {
        progressBar = findViewById(R.id.progressBar);
        progressBar.setVisibility(View.VISIBLE);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View customLayout = getLayoutInflater().inflate(R.layout.welcome_dialog, null);
        builder.setView(customLayout);

        TextView tvMessage = customLayout.findViewById(R.id.tvMessage);

        final SpannableString message = new SpannableString(getString(R.string.privacy_splashPage));


        setClickableSpan(message, getString(R.string.User_Agreement2), "https://blinkchat-privacy.chen88.top", tvMessage);
        setClickableSpan(message, getString(R.string.Privacy_Policy2), "https://blinkchat-privacy.chen88.top/terms-condition.html", tvMessage);

        tvMessage.setText(message);
        tvMessage.setMovementMethod(LinkMovementMethod.getInstance());
        tvMessage.setHighlightColor(Color.YELLOW);

        AlertDialog dialog = builder.setPositiveButton(R.string.Agree, (dialog12, which) -> {
                    SharedPreferences.Editor editor = getSharedPreferences("prefs", MODE_PRIVATE).edit();
                    editor.putBoolean("hasAgreed", true);
                    editor.apply();
                    dialog12.dismiss();
                    startMainActivity();
                })
                .setNegativeButton(R.string.Disagree, (dialog1, which) -> finish())
                .setCancelable(false)
                .create();

        dialog.show();
    }

    private void setClickableSpan(SpannableString spannableString, String target, String url, TextView textView) {
        int start = spannableString.toString().indexOf(target);
        int end = start + target.length();
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(true);
                ds.setColor(ContextCompat.getColor(SplashPage.this, R.color.blue));
            }
        }, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}
